﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Runtime.Serialization;
using Web.Framework.Svc;
using Web.Framework.Controls;
using System.Collections.Specialized;

namespace PlanProd.Contracts
{
    [Serializable]
    [DataContract]
    public class Temporada : IAuditable, ILogicalRemoved
    {
        public Temporada()
        {
            TemporadaSemanaMesList = new List<TemporadaSemanaMes>();
        }

        [DataMember]
        public int Id { get; set; }

        [DataMember]
        public string Nombre { get; set; }

        [DataMember]
        public string CodigoExterno { get; set; }

        [DataMember]
        public string MapeoTemporadaExterno { get; set; }
        
        [DataMember]
        public DateTime FechaDesde { get; set; }

        [DataMember]
        public DateTime FechaHasta { get; set; }

        [DataMember]
        public int EmpresaId { get; set; }

        [DataMember]
        public bool Activa { get; set; }

        [DataMember]
        public int NumeroSemanas { get; set; }

        [DataMember]
        public string LetraTemporada { get; set; }

        [DataMember]
        public List<TemporadaSemanaMes> TemporadaSemanaMesList { get; set; }

        [DataMember]
        public DateTime AuditFechaCreacion { get; set; }

        [DataMember]
        public string AuditUserNameCreacion { get; set; }

        [DataMember]
        public DateTime? AuditFechaModificacion { get; set; }

        [DataMember]
        public string AuditUserNameModificacion { get; set; }

        [DataMember]
        public bool Removed { get; set; }

        public TemporadaComercial ToTemporadaComercial()
        {
            TemporadaComercial tc = new TemporadaComercial();
            tc.FechaDesde = this.FechaDesde;
            tc.FechaHasta = this.FechaHasta;

            foreach (var tsm in TemporadaSemanaMesList)
            {
                Semana semana = new Semana();
                semana.Desde = tsm.Desde;
                semana.Hasta = tsm.Hasta;
                semana.NumeroSemana = tsm.NumeroSemana;
                semana.Anio = tsm.Anio;
                tc.Semanas.Add(semana);
            }

            return tc;
        }

        public static int GetCantidadSemanas(DateTime desde, DateTime hasta)
        {
            int semanas = 0;
            DateTime current = desde;
            while (current <= hasta)
            {
                semanas++;
                current = current.AddDays(7);
            }

            return semanas;
        }

        public void SortTemporadaDetalle()
        {
            this.TemporadaSemanaMesList.Sort((x, y) =>
            {
                if (x.Anio == y.Anio)
                {
                    return x.NumeroSemana - y.NumeroSemana;
                }
                else
                {
                    return x.Anio - y.Anio;
                }
            });
        }

        public HybridDictionary GetHybridNumeroSemanaOrden()
        {
            HybridDictionary hdictionary = new HybridDictionary();

            this.SortTemporadaDetalle();

            for (int i = 0; i < this.TemporadaSemanaMesList.Count; i++)
            {
                var detalleTemporada = this.TemporadaSemanaMesList[i];
                hdictionary.Add(detalleTemporada.NumeroSemana, i);
            }

            return hdictionary;
        }

        public HybridDictionary GetHybridOrdenNumeroSemana()
        {
            HybridDictionary hdictionary = new HybridDictionary();

            this.SortTemporadaDetalle();

            for (int i = 0; i < this.TemporadaSemanaMesList.Count; i++)
            {
                var detalleTemporada = this.TemporadaSemanaMesList[i];
                hdictionary.Add(i, detalleTemporada.NumeroSemana);
            }

            return hdictionary;
        }

        public HybridDictionary GetHybridNumeroSemanaAnio()
        {
            HybridDictionary hdictionary = new HybridDictionary();
            foreach (var detalleTemporada in this.TemporadaSemanaMesList)
            {
                hdictionary.Add(detalleTemporada.NumeroSemana, detalleTemporada.Anio);
            }

            return hdictionary;
        }
    }
}
